﻿
#region Namespaces
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//using WebReference;
using WebReference;
#endregion

public partial class Admin_AddReference : System.Web.UI.Page
{
    Service objServices = new Service();

    protected void Page_Load(object sender, EventArgs e)
    {
        
            if ((string)Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            bindDataGrid();
        
    }

    private void Clear()
    {
        txtAuthor.Text = string.Empty;
        txtCitation.Text = string.Empty;
        txtOraganization.Text = string.Empty;
        txtReferenceID.Text = string.Empty;
    }

    private void bindDataGrid()
    {
        dgReference.DataSource = objServices.GetReference().Tables[0];
        dgReference.DataBind();
    }
    
    protected void btnSave_Click1(object sender, EventArgs e)
    {
        if (txtAuthor.Text == string.Empty || txtCitation.Text == string.Empty || txtOraganization.Text == string.Empty)
        {
            lblMessage.Text = "All fields are mandatory";
        }
        else
        {
            string RefID = objServices.SaveReferenceUpdate(txtAuthor.Text, txtCitation.Text, txtOraganization.Text);
            if (RefID != string.Empty)
            {
                Session["ReferenceId"] = RefID;
                bindDataGrid();
                lblMessage.Text = "Reference Saved";
                btnAddExperimentData.Visible  = true;
                trAddReferenceTextbox.Visible = false;
                btnSave.Visible = false;
                trAddReference.Visible = true;
                btnEdit.Visible = true;
                Clear();
             }
            else
            {
                lblMessage.Text = "Error occured in save operation";
            }
        }
    }
    protected void btnAddExperimentData_Click(object sender, EventArgs e)
    {
        Response.Redirect("Material.aspx");
    }
    protected void dgReference_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgReference.PageIndex = e.NewPageIndex;
        bindDataGrid();
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        Response.Redirect("EditReference.aspx");
    }
}
